﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;
using System.Data;
using QLVMBAYDatacess;

namespace QLVMBAY.Pages
{
    public partial class pTimChuyenBay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session[ClientComm.KEY_TIM_VE] != null)
                {
                    var ttTim = (TTinTimVe)Session[ClientComm.KEY_TIM_VE];
                    lblChieuDi.Text = ttTim.TenTPDi + "     " + ttTim.TenTPDen;
                    var getCBays = ClientComm.Ints.ChuyenBay.GetListChuyenBay(ttTim.MaTPDi, ttTim.MaTPDen, ttTim.ThoiGianDi, ttTim.ThoiGianDen, ttTim.IsKhuHoi, true);

                    lblChieuVe.Text = ttTim.TenTPDen + "     " + ttTim.TenTPDi;
                    grdChuyenBayDi.DataSource = getCBays != null ? getCBays.Where(r => r.ChieuBay == 0).ToList() : null;
                    grdChuyenBayVe.DataSource = getCBays != null ? getCBays.Where(r => r.ChieuBay == 1).ToList() : null;
                    grdChuyenBayDi.DataBind();
                    grdChuyenBayVe.DataBind();
                    pnLuotVe.Visible = ttTim.IsKhuHoi;
                    lnkThanhToan.Enabled = Session[ClientComm.KEY_THANH_TOAN] != null;

                }
            }
        }        
        DataTable BuildDataTable(List<ChuyenBay> LstCBay,TTinTimVe ttTim)
        {
            var dt = CommonDO.LINQToDataTable(LstCBay.Select(r => new
            {
                r.MaChuyenBay,
                r.TenChuyenBay,
                r.ItemHangHangKhong.TenHang,
                TgKhoiHanh = r.TgKhoiHanh,
                ThoiGianBay = r.ThoiGianBay+" "
            }).ToList());
            var gets = new List<Ve_Ghe>();
            foreach (var ob in LstCBay)
            {
                var temp = ob.DsChiTietCB.Where(r => !(gets.Select(t => t.MaGheVe).Distinct()).Contains(r.ItemCTGheVe.MaGheVe)).Select(r=>r.ItemCTGheVe).ToList();
                gets = gets.Concat(temp).ToList();
            }            
            return dt;
 
        }
        int GetSLGhe(List<ChiTietChuyenBay> lstCT,string MaLoaiVe)
        {
            var get = lstCT.Where(r => r.ItemCTGheVe.ItemLoaiVe.MaLoaiVe == MaLoaiVe).Sum(r => r.SLGheConLai);
            return get;
        }
        protected void grdChuyenBayDi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                CallLoadCtlGrid(e);
                
            }
        }
        protected void rdChon1_CheckedChanged(object sender, EventArgs e)
        {
            CallLoad((RadioButton)sender);
        }
        protected void rdChon_CheckedChanged(object sender, EventArgs e)
        {
            CallLoad((RadioButton)sender);
        }
        void CallLoad(RadioButton ck)
        {           
            var ctl = (DropDownList)(ck).Parent.Parent.FindControl("ddlLoaiVe");
            if (ck.Checked == true)
            {

                ctl.Enabled = true;
                foreach (GridViewRow ob in grdChuyenBayDi.Rows)
                {
                    var item = ob.FindControl("rdChon") as RadioButton;
                    if (ck.Attributes["ID"] != item.Attributes["ID"])
                    {
                        item.Checked = false;
                        var ddl = ob.FindControl("ddlLoaiVe") as DropDownList;
                        ddl.Enabled = false;
                    }
                }
            }
            else
            {
                ctl.Enabled = false;
            }
        }
        string rChon = "rChon";
        int i = 1;
        private void CallLoadCtlGrid(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 var ChBay = (ChuyenBay)e.Row.DataItem;
                var lblSLVe = e.Row.FindControl("lblSLGhe") as Label;                
                var rd = e.Row.FindControl("rdChon") as RadioButton;
                rd.Attributes.Add("ID",rChon + i);
                i++;
                rd.GroupName = rChon;
                
               
                var lbl = e.Row.FindControl("lblTenHang") as Label;
                lbl.Text = ChBay.ItemHangHangKhong.TenHang;

                var lbl1 = e.Row.FindControl("lblTgKhoiHanh") as Label;
                lbl1.Text = ChBay.TgKhoiHanh.ToString("hh':'mm");

                var lbl2 = e.Row.FindControl("lblThoiGianBay") as Label;
                if (ChBay.ThoiGianBay.Days > 0)
                {
                    lbl2.Text = ChBay.ThoiGianBay.ToString("d'd hh'h'mm'p'");
                }
                else if (ChBay.ThoiGianBay.Hours > 0)
                {
                    lbl2.Text = ChBay.ThoiGianBay.ToString("hh'h'mm'p'");
                }
                else
                {
                    lbl2.Text = ChBay.ThoiGianBay.ToString("mm'p'");
                }
                var control = e.Row.FindControl("ddlLoaiVe") as DropDownList;
                
                if (control != null)
                {
                    control.Items.Clear();
                    var gets = ChBay.DsChiTietCB.Select(r => new {r.MaCTCBay,r.ItemCTGheVe.ItemLoaiGhe.TenLoaiGhe, r.ItemCTGheVe.ItemLoaiVe.TenLoaiVe, r.GiaVe ,r.SLGheConLai}).Distinct().ToList();
                    foreach (var ob in gets)
                    {
                        string text = ob.TenLoaiVe + " - "+ob.TenLoaiGhe+" (" + ob.GiaVe.ToString("#,##0") + " VNĐ)";
                        string value = ob.MaCTCBay.ToString();
                        int SLVe = ob.SLGheConLai;
                        if (SLVe == 0)
                        {
                            text += " (Hết vé)";
                            value = "-1";
                        }
                        else
                        {
                            text += " - (" + SLVe + " vé)";
                        }
                        var item = new ListItem(text, value);                        
                        control.Items.Add(item);
                    }
                    control.Enabled = false;
                }
            }
        }

        protected void btnTiepTuc_Click(object sender, EventArgs e)
        {
            if (Session[ClientComm.KEY_DANG_NHAP] != null)
            {
                var NgDung = (NguoiDung)Session[ClientComm.KEY_DANG_NHAP];
                if (NgDung.LoaiND == LOAI_NGUOI_DUNG.USER)
                {                

                    GridViewRow itemChonDi = null;
                    foreach (GridViewRow ob in grdChuyenBayDi.Rows)
                    {
                        var item = ob.FindControl("rdChon") as RadioButton;
                        if (item.Checked == true)
                        {
                            itemChonDi = ob;
                            break;
                        }
                    }
                    GridViewRow itemChonDen = null;
                    foreach (GridViewRow ob in grdChuyenBayVe.Rows)
                    {
                        var item = ob.FindControl("rdChon") as RadioButton;
                        if (item.Checked == true)
                        {
                            itemChonDen = ob;
                            break;
                        }
                    }
                    var ttTim=(TTinTimVe)Session[ClientComm.KEY_TIM_VE];
                    if (itemChonDi == null)
                    {
                        Page.RegisterStartupScript("Err",
                                                                             "<script language='javascript'>alert('Vui lòng chọn chuyến bay chiều đi.');</script>");
                        return;
 
                    }
                    else if (ttTim.IsKhuHoi && (itemChonDen == null || itemChonDi == null))
                    {
                        if (itemChonDi == null)
                        {
                            Page.RegisterStartupScript("Err",
                                                                                 "<script language='javascript'>alert('Vui lòng chọn chuyến bay chiều đi.');</script>");
                            return;
                        }
                        if (itemChonDen == null)
                        {
                            Page.RegisterStartupScript("Err",
                                                                                                           "<script language='javascript'>alert('Vui lòng chọn chuyến bay chiều về.');</script>");
                            return;
                        }
 
                    }
                    //CheckSLVe
                    var ddl = itemChonDi.FindControl("ddlLoaiVe") as DropDownList;
                    var getItem = ChiTietCBayDA.Inst.GetByPrimaryKey(true, ddl.SelectedValue);
                   
                    
                    if (getItem==null || getItem.SLGheConLai < (ttTim.SLVe + ttTim.SLVeTrEm))
                    {
                        Page.RegisterStartupScript("Err",
                                                                                "<script language='javascript'>alert('Vui lòng chọn chuyến bay chiều đi khác, vì số lượng vé còn lại không đủ.');</script>");
                        return;
                    }
                    ////else
                    ////{//(Hết vé)
                    ////    var ddl = itemChonDi.FindControl("ddlLoaiVe") as DropDownList;
                    ////    if (ddl.SelectedItem.Text.Contains("(Hết vé)"))
                    ////        {

                    ////        }
                    ////}
                    if (ttTim.IsKhuHoi)
                    {
                        ddl = itemChonDen.FindControl("ddlLoaiVe") as DropDownList;
                        getItem = ChiTietCBayDA.Inst.GetByPrimaryKey(true, ddl.SelectedValue);
                        if (getItem==null || getItem.SLGheConLai < (ttTim.SLVe + ttTim.SLVeTrEm))
                        {
                            Page.RegisterStartupScript("Err",
                                                                                    "<script language='javascript'>alert('Vui lòng chọn chuyến bay chiều về khác, vì số lượng vé còn lại không đủ.');</script>");
                            return;
                        }
                    }

                    Session[ClientComm.KEY_CHIEU_DI] = itemChonDi;
                    Session[ClientComm.KEY_CHIEU_VE] = itemChonDen;
                    Response.Redirect("~/Pages/pThanhToan.aspx");
                }
                else
                {
                    Page.RegisterStartupScript("Err",
                                                                          "<script language='javascript'>alert('Người dùng không được phép thực hiện chức năng mua vé.');</script>");
                    return;
                }

            }
            else
            {
                Page.RegisterStartupScript("Err",
                                                       "<script language='javascript'>alert('Vui lòng đăng nhập thực hiện chức năng mua vé.');</script>");
                return;
            }
        }

        protected void grdChuyenBayDen_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            CallLoadCtlGrid(e);
        }

        protected void lnkTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/./Default.aspx");
        }

        protected void lnkChuyenBay_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/pTimChuyenBay.aspx");
        }

        protected void lnkThanhToan_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/pThanhToan.aspx");
        }

        
    }
}